package app.fugema.shop.main.goods.adapter;

import com.base.common.app.LoginUtils;
import com.base.common.netBeanPackage.BaseApiResult;
import com.base.common.netBeanPackage.LiveDataWrapper;
import com.base.common.utils.UIUtils;
import com.base.common.view.adapter.ada.BaseRVAdapter;
import com.base.common.view.adapter.connector.BaseItemMultiType;
import com.base.common.view.adapter.connector.BaseViewHolder;
import com.base.common.viewmodel.BaseViewObserver;
import com.jeremyliao.liveeventbus.LiveEventBus;
import com.lljjcoder.style.citylist.Toast.ToastUtils;

import android.view.View;

import androidx.lifecycle.LifecycleOwner;
import app.fugema.shop.R;
import app.fugema.shop.databinding.ItemShopCouponListViewBinding;
import app.fugema.shop.main.goods.model.ShopCouponBean;
import app.fugema.shop.main.goods.vm.GoodsVM;

public class ShopCouponItemMultiType extends BaseItemMultiType<ShopCouponBean, ItemShopCouponListViewBinding>
{

    private GoodsVM goodsVM;
    private String userKey;

    public ShopCouponItemMultiType(LifecycleOwner lifecycleOwner, GoodsVM mGoodsVm)
    {
        super(lifecycleOwner);
        this.goodsVM = mGoodsVm;
        if(LoginUtils.isLogin())
        {
            userKey = LoginUtils.getToken();
        }
    }

    @Override
    public int getLayoutId()
    {
        return R.layout.item_shop_coupon_list_view;
    }


    @Override
    public void onBindViewHolder(ItemShopCouponListViewBinding binding, int position, BaseViewHolder viewHolder, ShopCouponBean bean)
    {
        super.onBindViewHolder(binding, position, viewHolder, bean);
        // 是否可被 领取    目前    1 可以领取    3 已领取
        switch (  bean.getReceive()  )
        {
            case 1:
                binding.btnApply.setVisibility(View.VISIBLE);
                binding.btnUsed.setVisibility(View.GONE);
                break;
            case 3:
                binding.btnApply.setVisibility(View.GONE);
                binding.btnUsed.setVisibility(View.VISIBLE);
                break;
            default:
                binding.btnApply.setVisibility(View.VISIBLE);
                binding.btnUsed.setVisibility(View.GONE);
                break;
        }

        if( UIUtils.isNotEmpty( bean.getEndTime() ) )
        {
            String lastTimeStr = bean.getEndTime().substring(0, bean.getEndTime().lastIndexOf('/')+3).toString().trim();
            binding.tvEndTime.setText("使用期限至 "+lastTimeStr);
        }

        if (viewHolder.getAdapter() != null)
        {
            /*
            int firstPos = viewHolder.getAdapter().getFirstPosition(BaseItemMultiType.TYPE_CHILD, 0);
            if (firstPos == position)
            {
                binding.clItemView.getDelegate().setCornerRadius_TR(DensityUtil.getDimens(R.dimen.dp_10));
                binding.clItemView.getDelegate().setCornerRadius_TL(DensityUtil.getDimens(R.dimen.dp_10));
            }
            else
            {
                binding.clItemView.getDelegate().setCornerRadius_TR(0);
                binding.clItemView.getDelegate().setCornerRadius_TL(0);
            }
            */
        }
    }

    @Override
    public void onItemClick(View view, BaseRVAdapter adapter, int onclickType, int position, ShopCouponBean bean)
    {
        super.onItemClick(view, adapter, onclickType, position, bean);
        switch (view.getId())
        {
            case R.id.btn_apply:
                //领取
                if( UIUtils.isNotEmpty( userKey ) )
                {
                    goodsVM.applyCoupon( bean.getShopId(),bean.getCouponId(),userKey )
                            .observe(getLifecycleOwner(), new BaseViewObserver<LiveDataWrapper<BaseApiResult>>()
                            {
                                @Override
                                protected void onSuccess(LiveDataWrapper<BaseApiResult> data)
                                {
                                      if( data.data.isSuccess() )
                                      {
                                          LiveEventBus.get("applySuccess",Integer.class).post( 1007 );
                                          ToastUtils.showShortToast( view.getContext() , "领取成功" );
                                      }
                                      else
                                      {
                                          ToastUtils.showShortToast( view.getContext() , data.data.getMsg() );
                                      }
                                }
                            });
                }
                break;
        }

    }
}
